
package com.jf.cloud.supplier.service.impl;

import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.supplier.constant.AuditStatus;
import com.jf.cloud.supplier.mapper.SupplierCompanyMapper;
import com.jf.cloud.supplier.model.SupplierCompany;
import com.jf.cloud.supplier.service.SupplierAuditingService;
import com.jf.cloud.supplier.service.SupplierCompanyService;
import com.jf.cloud.supplier.vo.SupplierAuditingVO;
import com.jf.cloud.supplier.vo.SupplierCompanyVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 供应商工商信息
 *
 * @author zz
 * @date 2022-03-14 17:04:32
 */
@Service
public class SupplierCompanyServiceImpl implements SupplierCompanyService {

    @Autowired
    private SupplierCompanyMapper supplierCompanyMapper;
    @Autowired
    private SupplierAuditingService supplierAuditingService;
    @Autowired
    private MapperFacade mapperFacade;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBySupplierId(SupplierCompany supplierCompany, Long supplierId, Integer temp) {
        this.checkInfo(supplierCompany, false);
        supplierCompanyMapper.updateBySupplierId(supplierCompany, supplierId, temp);
    }

    @Override
    public SupplierCompanyVO getById(Long supplierCompanyId) {
        SupplierCompany supplierCompany = supplierCompanyMapper.getBySupplierCompanyId(supplierCompanyId);
        return mapperFacade.map(supplierCompany, SupplierCompanyVO.class);
    }

    @Override
    public SupplierCompanyVO getSupplierCompanyBySupplierIdAndStatus(Long supplierId, Integer status) {
        SupplierCompanyVO supplierCompanyVO = supplierCompanyMapper.getSupplierCompanyBySupplierIdAndStatus(supplierId, status);
        // 检查工商信息是否有更新记录,若存在更新记录则发请求 查询审核信息 (逻辑上或许可以修改为存在多条工商信息记录)
        if (Objects.nonNull(supplierCompanyVO)) {
            Integer recordStatus = supplierCompanyMapper.getLatestRecord(supplierId);
            if (!Objects.equals(AuditStatus.SUCCESSAUDIT.value(), recordStatus)) {
                supplierCompanyVO.setEdit(true);
            }
        }
        return supplierCompanyVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SupplierCompany supplierCompany, boolean isCreate) {
        this.checkInfo(supplierCompany, isCreate);
        supplierCompanyMapper.save(supplierCompany);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SupplierCompany supplierCompany) {
        supplierCompanyMapper.update(supplierCompany);
    }

    @Override
    public Boolean checkCreditCode(String creditCode, Long supplierId) {
        Long supplierIdByCreditCode = supplierCompanyMapper.getSupplierIdByCreditCode(creditCode);
        if (Objects.nonNull(supplierIdByCreditCode)) {
            return !Objects.equals(supplierId, supplierIdByCreditCode);
        }
        return false;
    }

    @Override
    public void deleteBySupplierIdAndStatus(Long supplierId, Integer status) {
        supplierCompanyMapper.deleteBySupplierIdAndStatus(supplierId, status);
    }

    @Override
    public void batchUpdateStatus(Long supplierId, Integer status) {
        supplierCompanyMapper.batchUpdateStatus(supplierId, status);
    }

    /**
     * 校验店铺工商信息，isCreate为true代表校验新增时的信息，false代表校验更新时的信息
     * @param supplierCompany
     * @param isCreate
     */
    @Override
    public void checkInfo(SupplierCompany supplierCompany, Boolean isCreate) {
        if (isCreate) {
            SupplierCompanyVO oldCompany = supplierCompanyMapper.getSupplierCompanyBySupplierIdAndStatus(supplierCompany.getSupplierId(), AuditStatus.SUCCESSAUDIT.value());
            if (Objects.nonNull(oldCompany)) {
                throw new LuckException("店铺工商信息已存在,不能重复新增");
            }
        } else {
            SupplierAuditingVO supplierAuditingVO = supplierAuditingService.getBySupplierId(AuthUserContext.get().getTenantId());
            if (Objects.nonNull(supplierAuditingVO) && Objects.equals(supplierAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
                throw new LuckException("店铺处于待审核状态，无法修改店铺信息");
            }
        }
        //TODO 不校验营业执照号重复
//        if (checkCreditCode(supplierCompany.getCreditCode(), supplierCompany.getSupplierId())) {
//            throw new LuckException("统一信用码重复");
//        }
        if (supplierCompany.getEndTime() != null && !supplierCompany.getStartTime().before(supplierCompany.getEndTime())) {
            throw new LuckException("营业起始日期不能大于营业终止时间");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(SupplierCompany supplierCompany) {
        supplierCompanyMapper.updateById(supplierCompany);
    }

}
